package jfoo.detailsTable;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import jfoo.playlist.Playlist;
import audio.AudioFile;

public class DetailsTableModel extends AbstractTableModel {

	private Playlist playlist = null;
	private java.text.DecimalFormat nft;

	public Playlist getPlaylist() {
		return playlist;
	}

	public ArrayList<AudioFile> getAudioFiles() {
		return playlist.getAudioFiles();
	}

	public DetailsTableModel() {

		nft = new java.text.DecimalFormat("#0000.###");
		nft.setDecimalSeparatorAlwaysShown(false);
	}

	public Object getElementAt(int index) {

		return getAudioFiles().get(index);

	}

	public int getLastSelectedIndex() {
		if (playlist != null) {
			return playlist.getLastSelectedIndex();
		} else {
			return -1;
		}
	}

	public Object getValueAt(int r, int c) {

		Object val = null;
		AudioFile audioFile = getAudioFiles().get(r);
		// val = String.valueOf(audioFile.getFile().length() / 1024) + " Ko";
		switch (c) {
		case 0:
			val = nft.format(((Integer) r)).toString();
			break;
		case 1:
			val = audioFile.getArtist();
			if (val == null) {
				val = "";
			}
			break;
		case 2:
			val = audioFile.getTitle();
			if (val == null || ((String) val).equals("")) {
				val = audioFile.toString();
			}
			break;
		case 3:
			val = audioFile.getFormatedDuration();
			if (val == null) {
				val = "";
			}
			break;
		case 4:
			val = audioFile.getTrack();
			if (val == null || ((Integer) val == -1) || ((Integer) val == 0)) {
				val = "";
			}
			break;
		case 5:
			val = audioFile.getAlbum();
			if (val == null) {
				val = "";
			}
			break;
		case 6:
			val = audioFile.getGenre();
			if (val == null) {
				val = "";
			}
			break;
		case 7:
			SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy k:m");
			Date date = new Date(audioFile.getFile().lastModified());
			val = f.format(date);
			break;
		}

		return val;
	}

	public int getColumnCount() {
		return 8;
	}

	public void removeRow(AudioFile audioFile) {
		int index = getAudioFiles().indexOf(audioFile);
		getPlaylist().getAudioFiles().remove(getElementAt(index));
		this.fireTableRowsDeleted(index, index);
	}

	public void moveRow(int fromIndex, int toIndex) {
		AudioFile o = (AudioFile) getElementAt(fromIndex);
		getPlaylist().getAudioFiles().remove(fromIndex);
		this.fireTableRowsDeleted(fromIndex, fromIndex);

		if (toIndex > this.getRowCount()) {
			getPlaylist().getAudioFiles().add(o);
			this.fireTableRowsInserted(this.getRowCount() - 1, this.getRowCount() - 1);
		} else {
			getPlaylist().getAudioFiles().add(toIndex, o);
			this.fireTableRowsInserted(toIndex, toIndex);
		}

	}

	public void addAudioFiles(List<AudioFile> audioFiles, int index) {
		int rows = getRowCount();
		getAudioFiles().addAll(index, audioFiles);
		if (index == rows) {
			fireTableDataChanged(); // i don't know how to fire a new row when it's at the end ...
		} else {
			fireTableRowsInserted(index, index + audioFiles.size());
		}
	}

	public String getColumnName(int c) {
		String val = null;

		switch (c) {
		case 0:
			val = "";
			break;
		case 1:
			val = "Artist";
			break;
		case 2:
			val = "Title";
			break;
		case 3:
			val = "Time";
			break;
		case 4:
			val = "Track";
			break;
		case 5:
			val = "Album";
			break;
		case 6:
			val = "Genre";
			break;
		case 7:
			val = "Last Modified";
		}

		return val;
	}

	public int getRowCount() {
		if (playlist != null) {
			return getAudioFiles().size();
		} else {
			return 0;
		}
	}

	public void setValueAt(Object value, int row, int col) {
		AudioFile f = (AudioFile) getElementAt(row); // TODO : check if with file sorter it's ok
		String s = value.toString();

		switch (col) {
		case 0:
			return;
		case 1:
			f.setArtist(s);
			break;
		case 2:
			f.setTitle(s);
			break;
		case 3:
			return;
		case 4:
			f.setTrack(s);
			break;
		case 5:
			f.setAlbum(s);
			break;
		case 6:
			f.setGenre(s);
			break;
		case 7:
			return;
		}

		fireTableCellUpdated(row, col);
	}

	public void setPlaylist(Playlist playlist) {
		this.playlist = playlist;
		this.fireTableDataChanged();
	}

	public Class getColumnClass(int c) {

		return String.class;

	}

}
